/**
 * ScriptDev3 is an extension for mangos providing enhanced features for
 * area triggers, creatures, game objects, instances, items, and spells beyond
 * the default database scripting in mangos.
 *
 * Copyright (C) 2006-2013  ScriptDev2 <http://www.scriptdev2.com/>
 * Copyright (C) 2014-2020 MaNGOS <https://getmangos.eu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * World of Warcraft, and all World of Warcraft or Warcraft art, images,
 * and lore are copyrighted by Blizzard Entertainment, Inc.
 */

/**
 * ScriptData
 * SDName:      Ruins of Ahn'Qiraj
 * SD%Complete: 40
 * SDComment:   None
 * SDCategory:  Ruins of Ahn'Qiraj
 * EndScriptData
 */

/**
 * ContentData
 * mob_anubisath_guardian
 * EndContentData
 */

#include "precompiled.h"

/*######
## mob_anubisath_guardian
######*/
enum
{
    SPELL_METEOR                 = 24340,
    SPELL_PLAGUE                 = 22997,
    SPELL_SHADOW_STORM           = 26546,
    SPELL_THUNDER_CLAP           = 26554,
    SPELL_REFLECT_ARFR           = 13022,
    SPELL_REFLECT_FSSH           = 19595,
    SPELL_ENRAGE                 = 8599,
    SPELL_EXPLODE                = 25698,

    SPELL_SUMMON_ANUB_SWARMGUARD = 17430,
    SPELL_SUMMON_ANUB_WARRIOR    = 17431,

    EMOTE_FRENZY                 = -1000002,

    NPC_ANUB_WARRIOR             = 15537,
    NPC_ANUB_SWARM               = 15538
};

struct mob_anubisath_guardian : public CreatureScript
{
    mob_anubisath_guardian() : CreatureScript("mob_anubisath_guardian") {}

    struct mob_anubisath_guardianAI : public ScriptedAI
    {
        mob_anubisath_guardianAI(Creature* pCreature) : ScriptedAI(pCreature) { }

        uint32 m_uiSpell1;
        uint32 m_uiSpell2;
        uint32 m_uiSpell3;
        uint32 m_uiSpell4;
        uint32 m_uiSpell5;

        uint32 m_uiSpell1Timer;
        uint32 m_uiSpell2Timer;
        uint32 m_uiSpell5Timer;

        uint8 m_uiSummonCount;

        bool m_bIsEnraged;

        void Reset() override
        {
            m_uiSpell1 = urand(0, 1) ? SPELL_METEOR : SPELL_PLAGUE;
            m_uiSpell2 = urand(0, 1) ? SPELL_SHADOW_STORM : SPELL_THUNDER_CLAP;
            m_uiSpell3 = urand(0, 1) ? SPELL_REFLECT_ARFR : SPELL_REFLECT_FSSH;
            m_uiSpell4 = urand(0, 1) ? SPELL_ENRAGE : SPELL_EXPLODE;
            m_uiSpell5 = urand(0, 1) ? SPELL_SUMMON_ANUB_SWARMGUARD : SPELL_SUMMON_ANUB_WARRIOR;

            m_uiSpell1Timer = 10000;
            m_uiSpell2Timer = 20000;
            m_uiSpell5Timer = 10000;
            m_uiSummonCount = 0;
            m_bIsEnraged = false;
        }

        void Aggro(Unit* /*pWho*/) override
        {
            // spell reflection
            DoCastSpellIfCan(m_creature, m_uiSpell3);
        }

        void JustSummoned(Creature* pSummoned) override
        {
            pSummoned->AI()->AttackStart(m_creature->getVictim());
            ++m_uiSummonCount;
        }

        void SummonedCreatureDespawn(Creature* /*pDespawned*/) override
        {
            --m_uiSummonCount;
        }

        void DamageTaken(Unit* /*pDoneBy*/, uint32& /*uiDamage*/) override
        {
            // when we reach 10% of HP explode or enrage
            if (!m_bIsEnraged && m_creature->GetHealthPercent() < 10.0f)
            {
                if (m_uiSpell4 == SPELL_ENRAGE)
                {
                    DoScriptText(EMOTE_FRENZY, m_creature);
                    DoCastSpellIfCan(m_creature, m_uiSpell4);
                    m_bIsEnraged = true;
                }
                else
                {
                    DoCastSpellIfCan(m_creature->getVictim(), m_uiSpell4);
                }
            }
        }

        void UpdateAI(const uint32 uiDiff) override
        {
            if (!m_creature->SelectHostileTarget() || !m_creature->getVictim())
            {
                return;
            }

            // Meteor or Plague
            if (m_uiSpell1Timer < uiDiff)
            {
                DoCastSpellIfCan(m_creature->getVictim(), m_uiSpell1);
                m_uiSpell1Timer = 15000;
            }
            else
            {
                m_uiSpell1Timer -= uiDiff;
            }

            // Shadow Storm or Thunder Clap
            if (m_uiSpell2Timer < uiDiff)
            {
                DoCastSpellIfCan(m_creature->getVictim(), m_uiSpell2);
                m_uiSpell2Timer = 15000;
            }
            else
            {
                m_uiSpell2Timer -= uiDiff;
            }

            // summon Anubisath Swarmguard or Anubisath Warrior
            if (m_uiSpell5Timer < uiDiff)
            {
                // change for summon spell
                if (m_uiSummonCount < 4)
                {
                    DoCastSpellIfCan(m_creature->getVictim(), m_uiSpell5);
                }

                m_uiSpell5Timer = 15000;
            }
            else
            {
                m_uiSpell5Timer -= uiDiff;
            }

            DoMeleeAttackIfReady();
        }
    };

    CreatureAI* GetAI(Creature* pCreature) override
    {
        return new mob_anubisath_guardianAI(pCreature);
    }
};

void AddSC_ruins_of_ahnqiraj()
{
    Script* s;
    s = new mob_anubisath_guardian();
    s->RegisterSelf();

    //pNewScript = new Script;
    //pNewScript->Name = "mob_anubisath_guardian";
    //pNewScript->GetAI = &GetAI_mob_anubisath_guardian;
    //pNewScript->RegisterSelf();
}
